<template>
	<view class="content">
		<view class="mine_head">
			<view class="user_box" v-show="!log_state">
				<view class="user_msg">
					<img src="../../static/image/home_icon_photo.png" alt="" class="user_avatar">
					<view class="user_name">请登录您的账号</view>
				</view>
				<view class="login_btn" @click="go_login">点击登录</view>
			</view>
			<view class="user_box" v-show="log_state">
				<view class="user_msg">
					<img src="../../static/image/home_icon_photo.png" alt="" class="user_avatar">
					<text>{{username}}</text>
				</view>
				<text class="log_out_btn" @click="logout">退出登录</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				log_state: false,
				username: '',
				contact_phone: '',
			}
		},
		onLoad() {
			uni.getStorage({
				key: 'token',
				success: res => {
					this.log_state = true
					this.token = res.data
					this.mine_data(res.data)
				},
				fail: res => {
					this.log_state = false
				}
			})
		},
		methods: {
			// 联系我们弹窗
			contact_us () {
				uni.showModal({
					title:'提示',
					content: `您将呼叫：${this.contact_phone}`,
					showCancel: true,
					success: res => {
						if (res.confirm) {
							uni.makePhoneCall({
								phoneNumber: this.contact_phone
							})
						}
					}
				})
			},
			// 点击跳转寄件信息页
			go_sender_msg () {
				uni.navigateTo({
					url: '../senderMsg/senderMsg'
				})
			},
			// 点击跳转登录页
			go_login () {
				uni.redirectTo({
					url: '../login/login'
				})
			},
			// 调取首页信息
			mine_data (token) {
				this.$https.get(`api/mini_collection/v1/admin/mine?token=${token}`)
				.then(res => {
					console.log(res)
					this.username = res.data.username
					this.contact_phone = res.data.contact_us
				})
			},
			// 退出登录
			logout(){
				this.$https.delete(`api/mini_collection/v1/admin/logout?token=${this.token}`)
				.then(res => {
					// 退出登录成功 刷新页面 删除token
					this.log_state = false;
					uni.removeStorage({
						key: 'token',
						success: res => {
							// 退出登录后手动页面onload
							let page = getCurrentPages().pop();
							page.onLoad()
							uni.reLaunch({
								url: '/pages/mine/mine'
							})
						}
					})
				})
			}
		}
	}
</script>

<style>
	.content {
		height: 100%;
		width: 100%;
		overflow-x: hidden;
	}
	.mine_head{
		width: 100%;
		height: calc(216rpx - 48rpx);
		padding-top: 48rpx;
		background: linear-gradient(to right,#3BB0C3,#13A684);
	}
	.user_box{
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #FFFFFF;
		box-shadow: 4rpx 8rpx 20rpx 0 #0D7B62;
		border-radius: 12rpx;
		margin-left: 24rpx;
		width: 702rpx;
		height: 120rpx;
	}
	.user_msg{
		display: flex;
		align-items: center;
	}
	.user_avatar{
		width: 72rpx;
		height: 72rpx;
		margin-left: 24rpx;
		margin-right: 24rpx;
	}
	.user_name{
		font-size: 28rpx;
		color: #999999;
	}
	.login_btn{
		width: 170rpx;
		height: 72rpx;
		border-radius: 6rpx;
		text-align: center;
		line-height: 72rpx;
		color: white;
		background-color: #14A785;
		font-size: 28rpx;
		margin-right: 24rpx;
	}
	.log_out_btn{
		color: #FF4343;
		font-size: 28rpx;
		text-decoration: underline;
		margin-right: 24rpx;
	}
	.function_box{
		width: calc(100% - 48rpx);
		margin-left: 24rpx;
		padding-top: 20rpx;
	}
	.function_list{
		display: flex;
		width: 100%;
		background-color: #ffffff;
		margin-top: 24rpx;
		border-radius: 12rpx;
		align-items: center;
		justify-content: space-between;
		height: 100rpx;
	}
	.function_title_box{
		display: flex;
		align-items: center;
		margin-left: 24rpx;
	}
	.function_title_icon img{
		width: 100%;
		height: 52rpx;
	}
	.function_title_icon{
		width: 52rpx;
		height: 52rpx;
	}
	.next_icon{
		width: 24rpx;
		height: 24rpx;
		margin-right: 24rpx;
	}
	.function_title{
		line-height: 52rpx;
		margin-left: 24rpx;
		font-size: 28rpx;
	}
	.contact_us{
		display: flex;
		align-items: center;
	}
	.contact_us_phone{
		line-height: 100rpx;
		font-size: 28rpx;
		color: #333333;
		margin-right: 24rpx;
	}
</style>
